<?php
header("content-type:text/html;charset=utf-8");
include('common.php');
include(S_ROOT.'source/class_letao.php');
$letao = new LeTao($_SGLOBAL);
$op = isset($_GET['op']) ? trim($_GET['op']) : 'index';
set_time_limit(100);
switch($op)
{
	case 'index':
	if(submitcheck('dosubmit')) {
		$url = isset($_POST['url']) ? trim($_POST['url']) : '';
		if($url){
			showmessage('转入下载列表过程',"http://127.0.0.10/letao.php?op=getlist&url=$url");
		}
	}
	break;
	case 'clear':
		$sql = "TRUNCATE ".tname('curl_product').";";
		$_SGLOBAL['db']->query($sql);
		$sql = "TRUNCATE ".tname('curl_image').";";
		$_SGLOBAL['db']->query($sql);
		showmessage('清除成功，所有数据将会重新采集。',"letao.php");
	break;
	case 'brand':
		$url = 'http://www.letao.com/brand';
		
	break;
	case 'getlist':
		$url = isset($_GET['url']) ? trim($_GET['url']) : 'http://www.letao.com/shoe-4-1-a-103-a-a-a-a';
		if(empty($url))	showmessage('地址不得为空');
		$curl_page = $letao->getList($url);
		//如果$curl_page为真即有分页，跳转到分页采集
		if($curl_page){
			showmessage('抓取分页', "?op=getPage", 0.01);
		}else{
			showmessage('抓取产品', "?op=getProduct");
		}
	break;
	case 'getPage':
		
		$sql = 'SELECT url FROM '.tname('curl_page')." WHERE state=0";
		$query = $_SGLOBAL['db']->query($sql);
		$total = $_SGLOBAL['db']->num_rows($query);
		if($total){
			$url = $_SGLOBAL['db']->result($query, 0);
			$curl_page = $letao->getPage($url);
			updatetable('curl_page',array('state'=>1), array('url'=>$url));
			showmessage('继续下一页，剩余：'.$total, '?op=getPage');
		}else{
			//抓取产品页
			showmessage('抓取产品', '?op=getProduct', 0.01);
		}
	
		
	break;
	case 'getProduct':
		$sql = 'SELECT pid FROM '.tname('curl_product')." WHERE state=0";
		$query = $_SGLOBAL['db']->query($sql);
		$total = $_SGLOBAL['db']->num_rows($query);
		if($total){
			$pid = $_SGLOBAL['db']->result($query, 0);
			$curl_product = $letao->getProduct($pid);
			//exit('暂停');
			updatetable('curl_product',array('state'=>1), array('pid'=>$pid));
			showmessage('继续下一产品，剩余：'.$total, '?op=getProduct', 0.01);
		}else{
			//抓取产品页
			showmessage('商品采集完毕，进行图片本地化', '?op=getImage');
		}
	break;
	case 'getImage':
		$sql = 'SELECT i.id, i.pid, i.url, i.orders, p.goodsno, b.brandname FROM '.tname('curl_image')." i LEFT JOIN ".tname('curl_product')." p ON i.pid = p.pid LEFT JOIN ".tname('curl_brand')." b ON p.brandid = b.brandid WHERE i.state=0 ORDER BY id ASC LIMIT 10;";
		//exit($sql);
		$query = $_SGLOBAL['db']->query($sql);
		$total = $_SGLOBAL['db']->result($_SGLOBAL['db']->query('SELECT count(id) FROM '.tname('curl_image')." WHERE `state`=0"), 0);
		if($total){
			while($image = $_SGLOBAL['db']->fetch_array($query)){
				$curl_image = $letao->getImage($image);
				updatetable('curl_image',array('state'=>1), array('id'=>$product['id']));
			};
			
			showmessage('继续下一图片，剩余：'.$total, '?op=getImage', 0.01);
		}else{
			//抓取产品页
			showmessage('图片采集完毕，采集周期结束', '?op=index');
		}
	break;
	
	
	
	
	
}
include template('letao');